import { encrypt } from '../common/js/crypto'
const PASS_PREFIX = 'institutionEntry'
import { post, fetch } from '@/common/js/http'
import Cookies from 'js-cookie'
import * as types from './mutation-types'
import { login, logout } from '@/api/login'
import router from '@/router/index';
import { page, pageData, qlfhr, qlfhrData, user, processtype, usertype, Wgtype, saveAndStart, Nopower, NopowerData, damage, damageData, damagedict, damagsave, GetSerialkey, BarData, driver, facilities, flfacilitiesillegalsaveAndStart, facilitiesMsg, statisticaldetails,carqlfstatistics ,cardetails} from '@/api/HomePageSix'
// import {MessageBox, Message} from 'element-ui'
import { MessageBox } from 'mint-ui';
const actions = {
    //登录
    async login({ commit, state }, payload) {
        const { code, data, msg, token } = await post(login, payload)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg || '登录失败'
            })
        } else {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: '登录成功'
            })
            let date = new Date()
            date.setDate(date.getDate() + 30)
            window.sessionStorage.setItem('USERINFO', JSON.stringify(data))
            Cookies.set('JGRZTOKEN', data.token, { expires: date })//有效期一个月
            commit(types.SET_USER_INFO, data)
        }
        return data
    },
    // 退出
    async logout({ commit, state }) {
        await fetch(logout)
        window.sessionStorage.removeItem('USERINFO')
        Cookies.remove('JGRZTOKEN')
        commit(types.SET_USER_INFO, null)
        router.push('/login')
    },
    // GetSerialkey
    async GetSerialkey({ commit, state }, DataForm) {
        const { code, data, msg } = await fetch(GetSerialkey)
        if (code === 0) {
            return data
        } else {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg || '获取Serialkey失败'
            })
        }
    },
    // 获取page
    async GetTableDable({ commit, state }, DataForm) {
        const { code, data, msg } = await fetch(page + '?&limit=' + DataForm.limit + '&certType=' + DataForm.type + '&page=' + DataForm.page)
        if (code === 0) {
            return data
        } else {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg || '登录失败'
            })
        }
    },
    // 单页详情
    async pageIndex({ commit, state }, Id) {
        const { code, data, msg } = await fetch(pageData + Id)
        if (code === 0) {
            commit(types.VehicleData, data)
            return data
        }
    },
    // 档案列表
    async DriverfileData({ commit, state }, DataForm) {
        const { code, data, msg } = await fetch(qlfhr + '?&limit=' + DataForm.limit + '&certType=' + DataForm.type + '&page=' + DataForm.page)
        if (code === 0) {
            return data
        }
    },
    // 单页详情
    async DriverfileDataIndex({ commit, state }, Id) {
        const { code, data, msg } = await fetch(qlfhrData + Id)
        if (code === 0) {
            return data
        }
    },
    // 所有人员信息
    async user({ commit, state }, Id) {
        const { code, data, msg } = await fetch(user + Id)
        if (code === 0) {
            return data
        }
    },
    // 所有设备信息
    async facilities({ commit, state }, Id) {
        const { code, data, msg } = await fetch(facilities + Id)
        if (code === 0) {
            return data
        }
    },
    // 处理方式
    async processtype({ commit, state }, Id) {
        const { code, data, msg } = await fetch(processtype)
        if (code === 0) {
            return data
        }
    },
    // 人员类型
    async usertype({ commit, state }, Id) {
        const { code, data, msg } = await fetch(usertype)
        if (code === 0) {
            return data
        }
    },
    // 违规类型
    async Wgtype({ commit, state }, Id) {
        const { code, data, msg } = await fetch(Wgtype)
        if (code === 0) {
            return data
        }
    },
    // 人员违规登记
    async saveAndStart({ commit, state }, playload) {
        const { code, data, msg } = await post(saveAndStart, playload)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg || '提交失败'
            })

        } else {
            MessageBox.alert('提交成功').then(actions => {
                router.push('/')
            })
        }
    },
    // 无动力设备信息
    async Nopower({ commit, state }, DataForm) {
        const { code, data, msg } = await fetch(Nopower + '?&limit=' + DataForm.limit + '&certType=' + DataForm.type + '&page=' + DataForm.page)
        if (code === 0) {
            return data
        }
    },
    // 单页详情
    async NopowerIndex({ commit, state }, Id) {
        const { code, data, msg } = await fetch(NopowerData + Id)
        if (code === 0) {
            return data
        }
    },
    // 航空损伤信息
    async damage({ commit, state }, DataForm) {
        const { code, data, msg } = await fetch(damage + '?&limit=' + DataForm.limit + '&certType=' + DataForm.type + '&page=' + DataForm.page)
        if (code === 0) {
            return data
        }
    },
    // 单页详情
    async damagIndex({ commit, state }, Id) {
        const { code, data, msg } = await fetch(damageData + Id)
        if (code === 0) {
            return data
        }
    },
    // 下拉框
    async damagedict({ commit, state }) {
        const { code, data, msg } = await fetch(damagedict)
        if (code === 0) {
            return data
        }
    },
    // 航空记录表保存
    async damagsave({ commit, state }, DataForm) {
        const { code, data, msg } = await post(damagsave, DataForm)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg || '保存失败'
            })
        } else {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: '保存成功'
            })
        }
    },

    // Bar数据
    async BarData({ commit, state }, year) {
        const { code, data, msg } = await fetch(BarData + 'year=' + year)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg

            })
        } else {
            return data
        }
    },
    // 驾驶员实时统计列表
    async driver({ commit, state }, year) {
        const { code, data, msg } = await fetch(driver + '?&year=' + year)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg

            })
        } else {
            return data
        }
    },
    // 驾驶员实时统计列表
    async facilitiesMsg({ commit, state }, id) {
        const { code, data, msg } = await fetch(facilitiesMsg + id)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg

            })
        } else {
            return data
        }
    },
    // 违规登记表保存并发起一个流程
    async flfacilitiesillegalsaveAndStart({ commit, state }, DataForm) {
        const { code, data, msg } = await post(flfacilitiesillegalsaveAndStart, DataForm)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg || '保存失败'
            })
        } else {
            MessageBox.alert('提交成功').then(actions => {
                router.push('/')
            })
        }
    },
    // 驾驶员注销新增列表
    async statisticaldetails({ commit, state }, count, DataForm) {
        // console.log(DataForm)
        const { code, data, msg } = await fetch(statisticaldetails + '?year=' + count.year + '&column=' + count.column + '&type=' + count.type + '&limit=5' + '&page=' + count.page)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg

            })
        } else {
            return data
        }
    },
       // 车辆新增列表
       async carqlfstatistics({ commit, state }, year) {
        const { code, data, msg } = await fetch(carqlfstatistics + '?&year=' + year)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg

            })
        } else {
            return data
        }
    },
    // 车辆新增列表详情
    async cardetails({ commit, state }, count, DataForm) {
        // console.log(DataForm)
        const { code, data, msg } = await fetch(cardetails + '?year=' + count.year + '&column=' + count.column + '&type=' + count.type + '&limit=5' + '&page=' + count.page)
        if (code !== 0) {
            MessageBox({
                type: 'warning',
                title: '提示',
                message: msg

            })
        } else {
            return data
        }
    },
     
}

export default actions